package com.qwc.blog.web.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.common.constant.CommonConstant;
import com.qwc.blog.common.constant.ResponseConstant;
import com.qwc.blog.common.exception.ResponseException;
import com.qwc.blog.entity.Classify;
import com.qwc.blog.entity.dto.ClassifyDto;
import com.qwc.blog.entity.dto.ClassifyTreeDto;
import com.qwc.blog.entity.vo.ClassifyTreeVo;
import com.qwc.blog.service.IClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author qiuwencong
 * @Date 2022/2/14 14:12
 * @Package com.qwc.blog.web.admin
 * @Description
 */
@AdminController("/classify")
public class ClassifyController {

    @Autowired
    private IClassifyService classifyService;

    @GetMapping("/list")
    public ResponseEntity<Object> list(){
        List<Classify> list = classifyService.list(Wrappers.<Classify>lambdaQuery()
                .orderByAsc(Classify::getSort)
                .orderByDesc(Classify::getUpdateTime));
        return ResponseEntity.ok(ClassifyTreeVo.getTree(list));
    }

    @GetMapping("tree/{hasRoot}")
    public ResponseEntity<Object> getClassifyTree(@PathVariable Integer hasRoot){
        List<Classify> list = classifyService.list(Wrappers.<Classify>lambdaQuery()
                .eq(Classify::getStatus, CommonConstant.ABLE)
                .orderByAsc(Classify::getSort));
        return ResponseEntity.ok(ClassifyTreeDto.getTree(list,CommonConstant.ROOT,hasRoot));
    }

    @PostMapping
    public ResponseEntity<Object> add(@RequestBody @Validated ClassifyDto dto){
        Classify classify = new Classify();
        BeanUtil.copyProperties(dto,classify);
        return ResponseEntity.ok(classifyService.save(classify));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Object> get(@PathVariable @NotNull(message = "Id不能为空") Integer id){
        Classify byId = classifyService.getById(id);
        ClassifyDto dto = new ClassifyDto();
        BeanUtil.copyProperties(byId,dto);
        return ResponseEntity.ok(dto);
    }

    @PutMapping
    public ResponseEntity<Object> update(@RequestBody @Validated ClassifyDto dto){
        if (Objects.isNull(dto.getClassifyId())) {
            throw new ResponseException(ResponseConstant.ID_IS_NULL);
        }
        Classify classify = new Classify();
        BeanUtil.copyProperties(dto,classify);
        return ResponseEntity.ok(classifyService.updateById(classify));
    }

    @DeleteMapping("{ids}")
    public ResponseEntity<Object> deleteByIds(@PathVariable @NotNull(message = "请选择删除数据") Integer[] ids){
        return ResponseEntity.ok(classifyService.removeClassifyAndChildren(Arrays.asList(ids)));
    }

}
